function empresaChange() {
       request = createRequest();
	   var select = document.mainForm.FilialCombo;
	   var selectLength = select.options.length;
	   for (var i=0; i<select.options.length;i++){
		   select.options[i].text='';
		   select.options[i].value='';
        }
  	   for (var i=0; i<select.options.length;i++){
		   select.options[i] = null;
	   }
	  var url = "LoginForm.jsp"
      request.open("POST",url,false);
	  request.setRequestHeader("Content-Type",
	                           "application/x-www-form-urlencoded");
	  request.send("ACTION=gerarComboFiliais" +
	               "&EmpresaCombo="+document.mainForm.EmpresaCombo.value);
	   validarResposta(request);
	   var xmlDoc = request.responseXML;
	   var nodes = xmlDoc.getElementsByTagName("node");
	   for (var i = 0; i < nodes.length; i++) {
	       var empr_empresa  =nodes[i].getElementsByTagName("empr_empresa")[0].firstChild.nodeValue;
	       var empr_descricao  =nodes[i].getElementsByTagName("empr_descricao")[0].firstChild.nodeValue;
		   var preencherCombo = true;
		   var indexNum = select.options.length++;
		   select.options[indexNum].text=empr_descricao;
		   select.options[indexNum].value=empr_empresa;
	   }
}
function verificarLogin() {
      request = createRequest();
	  var url = "LoginForm.jsp"
      request.open("POST",url,false);
	  request.setRequestHeader("Content-Type",
	                           "application/x-www-form-urlencoded");
	  request.send("ACTION=verificarLogin" +
	               "&usur_login="+document.mainForm.j_username.value);
	   validarResposta(request);
	   var xmlDoc = request.responseXML;
	   var erro = xmlDoc.getElementsByTagName("erro")[0];
	   if (erro!=null){
          alertError(erro.firstChild.nodeValue);
		  document.getElementsByName("j_password")[0].onfocus=userFocus;
	   }
	   else{
	   	   document.getElementsByName("j_password")[0].onfocus=null;
   	   }
}
function verificarSenha() {
      request = createRequest();
	  var url = "LoginForm.jsp"
      request.open("POST",url,false);
	  request.setRequestHeader("Content-Type",
	                           "application/x-www-form-urlencoded");
	  request.send("ACTION=verificarSenha" +
	               "&usur_login="+document.mainForm.j_username.value +
	               "&usur_senha="+hex_md5(document.mainForm.j_password.value));
	  validarResposta(request);
	  var xmlDoc = request.responseXML;
	  var erro = xmlDoc.getElementsByTagName("erro")[0];
	  if (erro!=null){
         alertError(erro.firstChild.nodeValue);
	 	 document.getElementById("hidden").onfocus=senhaFocus;
      }
	  else{
	      document.getElementById("hidden").onfocus=submitFocus;
   	  }
}
function userFocus() {
	   document.getElementsByName("j_username")[0].focus();
}
function senhaFocus() {
	   document.getElementsByName("j_password")[0].focus();
}
function submitFocus(){
        document.getElementById("submit").focus();
	    //destravaSubmit();
}
function submeterTudo() {
      request = createRequest();
	  var url = "LoginForm.jsp"
      request.open("POST",url,false);
      if (document.mainForm.EmpresaCombo.value == ''||
         document.mainForm.EmpresaCombo.value == '0'){
         document.mainForm.EmpresaCombo.focus();
         return false;
      }
      if (document.mainForm.FilialCombo.value == ''){
         document.mainForm.FilialCombo.focus();
         return false;
      }      
      if (document.mainForm.j_username.value == ''){
         document.mainForm.j_username.focus();
         return false;
      }   
      if (document.mainForm.j_password.value == ''){
         document.mainForm.j_password.focus();
         return false;
      }   
	  request.setRequestHeader("Content-Type",
	                           "application/x-www-form-urlencoded");
	  request.send("EmpresaCombo="+document.mainForm.EmpresaCombo.value+
  	               "&FilialCombo="+document.mainForm.FilialCombo.value+
	               "&j_username="+document.mainForm.j_username.value +
	               "&j_password="+hex_md5(document.mainForm.j_password.value));
	  validarResposta(request);
	  var xmlDoc = request.responseXML;
	  if (xmlDoc==null){
         return false;
      }else{
	      destravaSubmit();
      }
}

function limparCampos() {
   window.location.href='./LoginForm.jsp'
}
